const context = {}

const config = require("../config")
context.config = config

/**
 * 成功返回
 * @param {any} data
 * @param {number} status {默认为200}
 */
context.success = function(data, status = 200) {
    const statusIsNumber = typeof status === "number"
    if (!statusIsNumber) {
        throw new Error("返回状态码status必须是number类型")
    }
    this.status = status
    this.body = { msg: "success", status, data }
}

/**
 * 失败返回
 * @param {any} reason
 * @param {number} status {默认为500}
 */
context.fail = function(reason, status = 500) {
    const statusIsNumber = typeof status === "number"
    if (!statusIsNumber) {
        throw new Error("返回状态码status必须是number类型")
    }
    this.status = status
    this.body = { msg: "fail", status, reason }
}

const model = require("../model")
context.model = model

module.exports = context